﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Noftware.Com.Cryptography;

namespace Noftware.Com.CryptographyTestLib
{
    /// <summary>
    /// Test class for testing key generation.
    /// </summary>
    [TestClass]
    public class TestKeyGeneration
    {
        [TestMethod]
        public void TestKeyGenerationNumeric()
        {
            var settings = new KeySettings()
            {
                KeyLength = 10,
                KeyType = KeyType.Numeric
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Key 10 chars in length
            Assert.AreEqual(settings.KeyLength, key.Length);

            // Key is numeric
            long num = 0;
            bool isNumeric = long.TryParse(key, out num);
            Assert.IsTrue(isNumeric);
        }

        [TestMethod]
        public void TestKeyGenerationAlphaNumeric()
        {
            var settings = new KeySettings()
            {
                KeyLength = 10,
                KeyType = KeyType.AlphaNumeric
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Key 10 chars in length
            Assert.AreEqual(settings.KeyLength, key.Length);
        }

        [TestMethod]
        public void TestKeyGenerationAlpha()
        {
            var settings = new KeySettings()
            {
                KeyLength = 10,
                KeyType = KeyType.Alpha
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Key 10 chars in length
            Assert.AreEqual(settings.KeyLength, key.Length);

            // Ensure each character is a letter
            foreach (char c in key)
            {
                Assert.IsTrue(char.IsLetter(c));
            }
        }

        [TestMethod]
        public void TestKeyGenerationGuid()
        {
            var settings = new KeySettings()
            {
                KeyType = KeyType.Guid,
                KeyCase = KeyCase.Upper
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Test to ensure it is a valid GUID
            Guid guid;
            bool isGuid = Guid.TryParse(key, out guid);
            Assert.IsTrue(isGuid);
        }

        [TestMethod]
        public void TestKeyGenerationUpperCase()
        {
            var settings = new KeySettings()
            {
                KeyLength = 10,
                KeyType = KeyType.Alpha,
                KeyCase = KeyCase.Upper
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Key 10 chars in length
            Assert.AreEqual(settings.KeyLength, key.Length);

            // Ensure each character is an uppercase letter
            foreach (char c in key)
            {
                Assert.IsTrue(char.IsLetter(c));
                Assert.IsTrue(char.IsUpper(c));
            }
        }

        [TestMethod]
        public void TestKeyGenerationLowerCase()
        {
            var settings = new KeySettings()
            {
                KeyLength = 10,
                KeyType = KeyType.Alpha,
                KeyCase = KeyCase.Lower
            };
            var gen = new Generator();
            string key = gen.Generate(settings);

            // Key 10 chars in length
            Assert.AreEqual(settings.KeyLength, key.Length);

            // Ensure each character is an uppercase letter
            foreach (char c in key)
            {
                Assert.IsTrue(char.IsLetter(c));
                Assert.IsTrue(char.IsLower(c));
            }
        }
    }
}
